package com.hyxt.DTO.protocols;

import com.hyxt.utils.bytes.BytesUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 是否华强协议判断类
 * @author shenp
 */
public class HUAQCase extends CommonCase implements ICase {
    private static Log log = LogFactory.getLog(HUAQCase.class);
    /**
     * 华强协议源数据上来的消息头
     */
    private byte fb = '*';
    /**
     * 华强协议源数据上来的消息尾
     */
    private byte lb = '#';
    /**
     * 华强协议源数据上来的消息头
     */
    private byte sfb = '[';
    /**
     * 华强协议源数据上来的消息尾
     */
    private byte slb = ']';
    /**
     * 设置set集合
     */
    private Set set;

    public HUAQCase() {

        set = new HashSet<String>();
        set.add("20");
        set.add("21");
        set.add("22");
    }
    @Override
    public boolean doCase(Object data) {
        Map<String, Object> map = new HashMap<String, Object>();
        byte[] bytes = (byte[]) data;
        new String(bytes);// *HQ20013466586906,BA&A03091639317467116435910624071213&B0000000000&F0000
        byte firstFlag = BytesUtil.getByte(0, bytes);// 42
        byte lastFlag = BytesUtil.getByte(bytes.length - 1, bytes);// 48
        byte[] providerData = BytesUtil.getWord(1, bytes);// 72, 81
        String providerName = new String(providerData);// HQ
        byte[] versionData = BytesUtil.getWord(3, bytes);// 50, 48
        String version = new String(versionData);// 20
        // 正常源数据
        if (firstFlag == fb && lastFlag == lb && "HQ".endsWith(providerName) && set.contains(version)) {
            log.debug((firstFlag == fb) + " " + (lastFlag == lb) + " " + providerName + " " + set.contains(version) + "!!!!!!!!!!!");
            return true;
        }
        // 正常源数据
        if (firstFlag == sfb && lastFlag == slb && "HQ".endsWith(providerName) && set.contains(version)) {
            return true;
        }
        return false;
    }

    @Override
    public byte getDataEnd() {
        return '#';
    }

    @Override
    public byte getDataHead() {
        return '*';
    }

    @Override
    public String getSign( byte[] data){
        // 获取sim卡号
        byte[] terminalID = BytesUtil.cutBytes(6, 11, data);
        String sim = new String(terminalID);
        return sim;
    }


    public static void main(String[] args) {
        HUAQCase huaqCase = new HUAQCase();
        byte[] bytes = "*HQ20015889603294,AA&A0732142233550011405829060520190600&B0000000000#".getBytes();
        //String sim = huaqCase.doCase(bytes);
    }
}
